$(function () {

    //点击获取验证码
    $('.getCode').on('tap', function () {
        //获取验证码
        $.ajax({
            url: '/user/vCode',
            type: 'get',
            data: '',
            dataType: 'json',
            success: function (data) {
                console.log(data.vCode)
                $('.getCode').html(data.vCode)
            }
        })
    })

    //点击按钮提交表单数据
    $('button').on('tap', function () {
        var name = $.trim($('[name="name"]').val())
        var phoneNumber = $.trim($('[name="phoneNumber"]').val())
        var password = $.trim($('[name="password"]').val())
        var confirmPassword = $.trim($('[name="confirmPassword"]').val())
        var code = $.trim($('[name="code"]').val())

        if (!name) {
            mui.toast('请输入用户名！')
            return false
        }
        if (!phoneNumber) {
            mui.toast('请输入手机号！')
            return false
        }
        if (phoneNumber.length < 11) {
            mui.toast('请输入合法手机号！')
            return false
        }
        if (!password) {
            mui.toast('请输入密码！')
            return false
        }
        if (!confirmPassword) {
            mui.toast('请确认密码！')
            return false
        }
        if (password !== confirmPassword) {
            mui.toast('两次输入的密码不一样！')
            return false
        }
        if (!code) {
            mui.toast('请输入验证码！')
            return false
        }
        console.log(name)
        console.log(password)
        console.log(phoneNumber)
        console.log(code)
        $.ajax({
            url: '/user/register',
            type: 'post',
            data: {
                username: name,
                password: password,
                mobile: phoneNumber,
                vCode: code
            },
            success: function (data) {
                console.log(data)
                if (data.success == true){
                    mui.toast('注册成功,请登录')
                    location.href = 'login.html'
                } else if (data.error == 401) {
                    mui.toast('验证码错误')
                }if (data.error == 403 ) {
                    mui.toast('用户名已存在！')
                }
            },
            error: function () {
                mui.toast('网络错误')
            }
        })
    })

})